$(function() {       
    'use strict';
    require.config({
        baseUrl: './js',
        urlArg: 'v=1.0',
        paths: {        
            'angular': '../bower_components/angular/angular',
            'angular-route': '../bower_components/angular-route/angular-route',
            'angular-resource': '../bower_components/angular-resource/angular-resource'
        },
        shim: {
            'angular': {exports: 'angular'},        
            'angular-resource': {
                deps: ['angular'],
                exports: 'angular-resource'
            },
            'WebPDF' : {
                deps: ['angular', 'angular-resource']
            }
        },
        packages : ["PDFView", "Plugins/Form", "Plugins/Hand", "Plugins/Annot"]
    });
    
    require(['PDFViewer'], function() {
        var settings = {
            s3Key: UCX_Utils.getUrlParam("doc")
        };
        var pdfApi = $("#docViewer").pdfViewer(settings).data("jsp");
      

        pdfApi.curPageChanged(function(event, data) {          
            $("#pageIndexInputSelect").val(data.curIndex);
        });

     
        pdfApi.scaleChanged(function(event, data) {          
            $('.btnSelectZoom').val(data.scale);
        });


        pdfApi.pageCountChanged(function(event, data){
           var select = $("#pageIndexInputSelect").empty();         
           var option = $("<option>");
           var pageCount = data.pageCount;
           for (var index = 0; index < pageCount; index++) {
                select.append(option.clone().text(index + 1 + " / " + pageCount).attr('value', index));
           }          
        });


        var $window = $(window);
        var $containerMenu = $("#docDiv");
        var $bottomControl = $("#bottomControl");

        $(window).on("resize", function() {
            var height = $window.height() - $containerMenu.height() - $bottomControl.height() - 101;
            $("#docViewer").height(height);                       
            pdfApi.layout({
                    'height': $("#docViewer").height(),
                    'width': $("#docViewer").width()
            });                    
        });

        $(window).trigger("resize");

       //pdfApi.setScale({'scale': '0.5'});

        $('.btnSelectZoom').on('change', function() {
            pdfApi.setScale({
                'zoomMode': $(this).val()
            });
        });

        $('#btnGoToNextPage').on("click", function() {
            pdfApi.nextPage();
        });

        $('#btnGoToPreviousPage').on("click", function() {
            pdfApi.previousPage();
        });

        $('.fwr-goToPage-input').on('change', function() {
             pdfApi.gotoPage({
                'pageIndex': $(this).val()
            });
        });

        $('.btnHandTool').on('click', function() {
            pdfApi.setCurrentToolByName("handTool");
        });

        $("#btnTypewriter").on('click', function() {
             pdfApi.setCurrentToolByName("annotTool");
        });
    });
});
